<?php

class moderator extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('moderator_model');
        if($this->session->userdata('user_id')==null){
            redirect(base_url());
        }else if($this->session->userdata('type')!= 'moderator'){
            redirect(base_url()); 
        }
    }

    public function dashboard($tab = '') {
        $id = $this->session->userdata('user_id');
        if (!empty($tab)) {
            if ($tab == 'deactivatestud' || $tab == 'reactivatestud' || $tab == 'reset' || $tab == 'editStud' || $tab == 'addStud') {
                $data['activeMenu'] = 'myStudents';
                if ($tab == 'deactivatestud') {
                     $data['activeMenuTab'] = 'activeStudents';
                    $data['alert'] = 'Successfully deactivated user.';
                } else if ($tab == 'reactivatestud') {
                    $data['activeMenuTab'] = 'inactiveStudents'; 
                    $data['alert'] = 'Successfully reactivated user.';
                } else if ($tab == 'reset') {
                     $data['activeMenuTab'] = 'activeStudents';
                    $data['alert'] = 'Password reset was successful.';
                } else if ($tab == 'editStud') {
                     $data['activeMenuTab'] = 'activeStudents';
                    $data['alert'] = 'Successfully updated user.';
                } else if ($tab == 'addStud') {
                     $data['activeMenuTab'] = 'activeStudents';
                    $data['alert'] = 'Successfully added user.';
                }
            } else if ($tab == 'changepass' || $tab == 'profileupdate' || $tab == 'errpass' || $tab=='profile') {
                $data['activeMenu'] = 'myProfile';
                if ($tab == 'changepass') {
                    $data['alert'] = 'Successfully updated password.';
                } else if ($tab == 'profileupdate') {
                    $data['alert'] = 'Successfully updated profile.';
                } else if ($tab == 'errpass') {
                    $data['error'] = 'Old password entered is invalid.';
                }
            }else if ($tab == 'addsec' ||  $tab == 'editsec' || $tab == 'deactivatesect' || $tab == 'reactivatesect'){
                $data['activeMenu'] = 'mySections';
                $data['activeMenuTab'] = 'activeSections';
                 if ($tab == 'addsec') {
                    $data['alert'] = 'Successfully added section.';
                } else if($tab == 'deactivatesect') {
                    $data['alert'] = 'Successfully deactivated section.';
                }else if ($tab == 'reactivatesect'){
                    $data['alert'] = 'Successfully reactivated section.';
                }else if ($tab == 'editsec'){
                    $data['alert'] = 'Successfully updated section.';
                }
            }
        }
        $data['profile'] = $this->moderator_model->getModerator($id);
        $data['myStudents'] = $this->moderator_model->getMyStudents($id,'N');
        $data['inactive'] = $this->moderator_model->getMyStudents($id,'Y');
        $data['title'] = "Moderator's Dashboard";
        $data['pie'] = $this->getSectionChart();
//       var_dump($data['pie']);
//       die();
        $data['activeSections'] = $this->moderator_model->getStudentCountperSection($id);
        $data['inactiveSections'] = $this->moderator_model->getSections($id,'Y');
        $this->load->view('templates/header', $data);
        $this->load->view('moderator', $data);
        $this->load->view('templates/footer', $data);
    }

    public function getSectionChart() {
        $id = $this->session->userdata('user_id');
        $sections = $this->moderator_model->getSections($id,'N');
        if(!empty($sections)){
        foreach ($sections as $section) {
            $datus = $this->moderator_model->pieChart($section['section_id'], $section['section_name']);
            if (!empty($datus)) {
                $data[] = $datus;
            }
        }}
        
        if(isSet($data)){
            return $data;
        }else return;
        
    }

    public function edit() {
        $userid = $this->session->userdata('user_id');
        $data['profile'] = $this->moderator_model->getModerator($userid);
        $data['title'] = 'Edit Profile';
        $data['cancel_link'] = base_url('/moderator/dashboard/');
        $data['profile_link'] = base_url('/moderator/dashboard/profile');

        $this->load->helper('form', 'url');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('firstname', 'First Name', 'trim|required');
        $this->form_validation->set_rules('middlename', 'Middle Name', 'trim');
        $this->form_validation->set_rules('lastname', 'Last Name', 'trim|required');

        if ($this->form_validation->run() === FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('EditProfile', $data);
            $this->load->view('templates/footer');
        } else {
            $firstname = ucwords(strtolower($this->input->post('firstname')));
             $middlename = ucwords(strtolower($this->input->post('middlename')));
             $lastname = ucwords(strtolower($this->input->post('lastname')));
            $studentdata = array('firstname' => $firstname, 'middlename' =>  $middlename, 'lastname' => $lastname);
            $this->moderator_model->updateModerator($userid, $studentdata);
            redirect(base_url('/moderator/dashboard/profileupdate'));
        }
    }

    public function editStudent($studid) {
        $userid = $this->session->userdata('user_id');
        $data['profile'] = $this->moderator_model->getModerator($userid);
        $data['student_profile'] = $this->moderator_model->getStudent($studid);
        $data['sections'] = $this->moderator_model->getSections($userid,'N');
        $data['cancel_link'] = base_url('/moderator/dashboard/');
        $data['profile_link'] = base_url('/moderator/dashboard/profile');
        $data['title'] = 'Edit Student';
        $this->load->helper('form', 'url');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('firstname', 'First Name', 'trim|required');
        $this->form_validation->set_rules('middlename', 'Middle Name', 'trim');
        $this->form_validation->set_rules('lastname', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('section', 'Section', 'required');

        if ($this->form_validation->run() === FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('EditStudent', $data);
            $this->load->view('templates/footer');
        } else {
            $firstname = ucwords(strtolower($this->input->post('firstname')));
             $middlename = ucwords(strtolower($this->input->post('middlename')));
             $lastname = ucwords(strtolower($this->input->post('lastname')));
            $studentdata = array('firstname' => $firstname, 'middlename' =>  $middlename, 'lastname' => $lastname, 'section_id' => $this->input->post('section'));
            $this->moderator_model->updateStudent($studid, $studentdata);
            redirect(base_url('/moderator/dashboard/editStud/'));
        }
    }

    public function addStudent() {
        $userid =$this->session->userdata('user_id');
        $data['profile'] = $this->moderator_model->getModerator($userid);
        $data['sections'] = $this->moderator_model->getSections($userid,'N');
        $data['cancel_link'] = base_url('/moderator/dashboard/');
        $data['profile_link'] = base_url('/moderator/dashboard/profile');
        $data['title'] = 'Add Student';
        $this->load->helper('form', 'url');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('firstname', 'First Name', 'trim|required');
        $this->form_validation->set_rules('middlename', 'Middle Name', 'trim');
        $this->form_validation->set_rules('lastname', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('section', 'Section', 'required');

        if ($this->form_validation->run() === FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('AddStudent', $data);
            $this->load->view('templates/footer');
        } else {
            $firstname = ucwords(strtolower($this->input->post('firstname')));
             $middlename = ucwords(strtolower($this->input->post('middlename')));
             $lastname = ucwords(strtolower($this->input->post('lastname')));
            $username = strtolower(substr($this->input->post('firstname'), 0, 1) . $this->input->post('lastname') . rand(1, 500));
            $userdata = array('username' => $username, 'type' => 'student');
            $studentID = $this->moderator_model->addUser($userdata);
            $studentdata = array('user_id' => $studentID, 'firstname' => $firstname, 'middlename' => $middlename, 'lastname' => $lastname, 'section_id' => $this->input->post('section'));
            $this->moderator_model->AddStudent($studentdata);
            $this->addStatus($studentID);
            redirect(base_url('/moderator/dashboard/addStud/'));
        }
    }

    public function deactivateStudent($userid) {
        $userdata = array('isDeleted' => 'Y');
        $this->moderator_model->updateUser($userid, $userdata);
        redirect(base_url('/moderator/dashboard/deactivatestud'));
    }
    
    public function deactivateSection($section_id) {
        $data = array('isDeactivated' => 'Y');
        $this->moderator_model->updateSection($section_id, $data);
        redirect(base_url('/moderator/dashboard/deactivatesect'));
    }
    
    public function reactivateSection($section_id) {
        $data = array('isDeactivated' => 'N');
        $this->moderator_model->updateSection($section_id, $data);
        redirect(base_url('/moderator/dashboard/reactivatesect'));
    }

    public function changePassword($userid) {
        $moderator_profile = $this->moderator_model->getModerator($userid);
        $password = trim($moderator_profile['password']);
        $oldpass = trim($this->input->post('currentPass'));
        $newpass = trim($this->input->post('newPass'));
        if ($password == $oldpass) {
            $userdata = array('password' => $newpass);
            $this->moderator_model->updateUser($userid, $userdata);
            redirect(base_url('/moderator/dashboard/changepass'));
        } else {
            redirect(base_url('/moderator/dashboard/errpass'));
        }
    }
    
    public function addSection($userid){
        $sectionName = trim($this->input->post('sectionName'));
        $data = array('user_id' => $userid,'section_name'=>$sectionName);
        $this->moderator_model->addSection($data);
        redirect(base_url('/moderator/dashboard/addsec'));
    }

    public function reactivateStudent($userid) {
        $userdata = array('isDeleted' => 'N');
        $this->moderator_model->updateUser($userid, $userdata);
        redirect(base_url('/moderator/dashboard/reactivatestud'));
    }

    public function resetPassword($userid) {
        $userdata = array('password' => '123');
        $this->moderator_model->updateUser($userid, $userdata);
        redirect(base_url('/moderator/dashboard/reset'));
    }

    public function addStatus($userid) {
        $counter = 1;
        while ($counter < 10) {
            $this->moderator_model->addStatus(array('user_id' => $userid, 'criteria_id' => $counter, 'doesExist' => 'Y', 'state' => 'B'));
            $counter +=1;
        }
    }
    
    public function updateSection($section_id){
        $sectionName = trim($this->input->post('secName'));
        $data = array('section_name' => $sectionName);
        $this->moderator_model->updateSection($section_id,$data);
        redirect(base_url('/moderator/dashboard/editsec'));
    }

}

?>
